(function(undefined) {

var createHtml = function(el) {
		return document.createElement(el);
	},
	getBody = function() {
		return document.body;
	};
var win = {
	createTitle: function(title) {
		var el1, el2;
		el1 = createHtml('div');
		el2 = createHtml('div');
		el1.innerHTML = title;
		el1.className = 'title';
		el2.appendChild(el1);
		el2.className = 'wtitle';
		return el2;
	},
	createContent: function(content) {
		var el = createHtml('div');

		el.appendChild(content);
		el.className = 'content word-wrap'
		return	el;
	},
	createModal: function(arrEl) {
		var el = createHtml('div');
		el.className = 'modal';
		$.each(arrEl, function(i, v) {
			el.appendChild(v);
		});
		return el;
	},
	createForm: function(fields, buttons) {
		var form = createHtml('div'),
			tbl = createHtml('table');

		$.each(fields, function(i, f) {
			var tr = createHtml('tr');

			if($.isArray(f)) {
				$.each(f, function(i, f) {
					var td = createHtml('td'),
						input = createHtml('input'),
						type = f.type ? f.type : 'text';

					input.setAttribute('type', type);
					input.setAttribute('name', f.name);
					input.setAttribute('class', 'field');

					if(f.field) {
						td.appendChild("<span>" + f.field + ":&nbsp;</span>");
					}
					tr.appendChild(td);
					td = createHtml('td');
					td.appendChild(input);
					tr.appendChild(td);
				});
			} else {
				var td = createHtml('td');
					input = createHtml('input'),
					type = f.type ? f.type : 'text';

				input.setAttribute('type', type);
				input.setAttribute('name', f.name);
				input.setAttribute('class', 'field');

				if(f.field) {
					var sp = createHtml('span');
					sp.innerHTML = f.field;
					td.appendChild(sp);
				}
				tr.appendChild(td);
				td = createHtml('td');
				td.appendChild(input);
				tr.appendChild(td);
			}
			tbl.appendChild(tr);
		});

		form.appendChild(tbl);

		if(buttons) {
			form.appendChild(createHtml('hr'));

			$.each(buttons, function(i, btn) {
				var b = createHtml('input'),
					name = btn.name,
					value = btn.value;

				b.setAttribute('type', 'button');
				b.setAttribute('name', name);
				b.setAttribute('value', value ? value : name);

				if(btn.handler) {
					b.addEventListener('click', btn.handler, true);
				}

				form.appendChild(b);
			});
		}

		return form;

		/*var form = '<table>', type = 'text';
		$.each(fields, function(i, f) {
			type = f.type ? f.type : 'text';
			form += '<tr><td><span>' + f.field + ':&nbsp;</span></td>';
			form += "<td><input type='" + type	+ "'' name='" + f.name +"' /></tr>";
		});

		form += '</table>';

		if(buttons) {
			form += "<hr><div style='align: right;' class='rightalign'>";
			$.each(buttons, function (i, btn) {
				var name = btn.name;

				form += '&nbsp;';
				form += "<input class='rightalign' type='button' name='" + name + "' value='" + name + "' onClick='" + btn.handler + "();'/>";
			})
			form += '</div>';
		}

		return	 form;*/
	}
};

var textblast = {

	Form: function(o) {
		var mask = document.createElement('div');
			mask.className = 'mask';

			document.body.appendChild(mask);
			
		var form = win.createModal([win.createTitle(o.title), win.createContent(win.createForm(o.fields, o.buttons))]);
		o.renderTo.appendChild(form);

		return form;
	},
	postReq: function(o) {
		$.ajax({
			url: o.url,
			data: o.params,
			type: 'post',
			success: o.success,
			error: o.error
		});
	},
	getReq: function(o) {
		$.ajax({
			url: o.url,
			data: o.params,
			type: 'get',
			success: o.success,
			error: o.error
		});
	}
};

window.tb = textblast;

tb.Modal = function() {

};

tb.Button = (function() {
	var fn = function() {
			this.init	
		},
		proto = fn.prototype;

	proto.init = function(parent) {

	};

	return fn;
})();

})();